package cn.edu.hust.cm.vs;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Pair;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.Random;

import cn.edu.hust.cm.vs.BlurImageFragment.OnFragmentInteractionListener;

/**
 * 1. KM Android实时毛玻璃效果探索
 * 2. https://github.com/kikoso/android-stackblur
 * 3. https://github.com/Commit451/NativeStackBlur https://github.com/kikoso/android-stackblur/issues/26
 */
public class BlurImageActivity extends AppCompatActivity implements OnFragmentInteractionListener {

    private static final String IMG_URL = "https://www.baidu.com/img/bd_logo1.png";

    private Random mRandom;
    private String mImgUrl = IMG_URL;

    private TextView mTvUrl;
    private TextView mTvSize;
    private TextView mTvDimen;

    private TextView mTvFastBlur;
    private TextView mTvRsBlur;
    private TextView mTvStackBlur;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blur_image);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TextView title = (TextView) toolbar.findViewById(R.id.text_activity_title);
        title.setText(getTitle());

        setupViews();

        mRandom = new Random();
        mImgUrl = nextImgUrl();
        loadImage(mImgUrl);
    }

    private void setupViews() {
        mTvUrl = (TextView) findViewById(R.id.tv_url);
        mTvSize = (TextView) findViewById(R.id.tv_size);
        mTvDimen = (TextView) findViewById(R.id.tv_dimen);

        mTvFastBlur = (TextView) findViewById(R.id.tv_fast_blur);
        mTvRsBlur = (TextView) findViewById(R.id.tv_rs_blur);
        mTvStackBlur = (TextView) findViewById(R.id.tv_stack_blur);
    }

    private void loadImage(final String url) {
        Glide.with(this).load(url).asBitmap().skipMemoryCache(false)
                .into(new SimpleTarget<Bitmap>(135, 135) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        mTvUrl.setText(url);
                        mTvSize.setText(String.valueOf(getByteCount(resource)));
                        mTvDimen.setText(resource.getWidth() + "x" + resource.getHeight());

                        doFastBlur(resource);
                        doRsBlur(resource);
                        doStackBlur(resource);
                    }
                });
    }

    private void doFastBlur(final Bitmap bitmap) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Pair<Bitmap, String> res = BlurImages.doFastBlur(bitmap);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvFastBlur.setText(res.second);
                    }
                });
            }
        }).start();
    }

    private void doRsBlur(final Bitmap bitmap) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Pair<Bitmap, String> res = BlurImages.doRsBlur(BlurImageActivity.this, bitmap);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvRsBlur.setText(res.second);
                    }
                });
            }
        }).start();
    }

    private void doStackBlur(final Bitmap bitmap) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Pair<Bitmap, String> res = BlurImages.doStackBlur(bitmap);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvStackBlur.setText(res.second);
                    }
                });
            }
        }).start();
    }

    private int getByteCount(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return bitmap.getAllocationByteCount();
        } else {
            return bitmap.getByteCount();
        }
    }

    private String nextImgUrl() {
        return BlurImages.IMAGE_SET[mRandom.nextInt(BlurImages.IMAGE_SET.length)];
    }

    public void onClick(View view) {
        BlurImageFragment fragment = null;

        switch (view.getId()) {
            case R.id.btn_original:
                fragment = BlurImageFragment.newInstance(mImgUrl, BlurImages.BLUR_NONE);
                break;

            case R.id.btn_fast_blur:
                fragment = BlurImageFragment.newInstance(mImgUrl, BlurImages.BLUR_FAST);
                break;

            case R.id.btn_rs_blur:
                fragment = BlurImageFragment.newInstance(mImgUrl, BlurImages.BLUR_RS);
                break;

            case R.id.btn_stack_blur:
                fragment = BlurImageFragment.newInstance(mImgUrl, BlurImages.BLUR_STACK);
                break;

            case R.id.tv_next_image:
                mImgUrl = nextImgUrl();
                loadImage(mImgUrl);
                break;
        }

        if (fragment != null) {
            BlurImageFragment prev = (BlurImageFragment) getSupportFragmentManager().findFragmentByTag("dialog");
            if (prev != null) {
                prev.dismiss();
            }
            fragment.show(getSupportFragmentManager(), "dialog");
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        // NO OP
    }
}
